<?php
    function connect() {
        $host = "mysql1.000webhost.com";
        $database = "a9343692_ics414";
        $username = "a9343692_ics414";
        $password = "ics414";
        
        // Connect to server and select databse.
        if (!mysql_connect($host, $username, $password)) {
            die('Could not connect to server.');
        }
        
        if (!mysql_select_db($database)) {
            die('Could not connect to database.');
        }
        
    }
    
    function disconnect() {
        if (!mysql_close()) {
            die('Could not disconnect from server.');
        }
    }
    
    function getGameList($filter) {
        $query = "SELECT * FROM games ORDER BY ";
        switch($filter) {
            case 'Top Rated' :
                $query .= "user_rating DESC";
                break;
            case 'Most Favorited' :
                $query .= "num_favorite DESC";
                break;
                break;
            case 'Most Popular' :
                $query .= "num_views DESC";
                break;
            case 'Coming Soon' :
                break;
        }
        $result = mysql_query($query) or die(mysql_error());
        $gameList = array($filter . ' Games List');
        // Store the results of the query
        while ($row = mysql_fetch_array($result)) {
            $gameList[] = $row['game_name'];
        }
        return $gameList;
    }
    
    function getGameRatings($gameName)  {
        $query = 'SELECT * FROM games WHERE game_name = "';
        $query .= $gameName . '"';
        $result = mysql_query($query) or die(mysql_error());
        $gameReviews = array($gameName. ' Reviews');
        //Store the results
        $row = mysql_fetch_array($result);
        $gameReviews[] = $row['user_rating'] . ";" . $row['metacritic_rating'] . ";" . $row['gamespot_rating'] . ";" . $row['ign_rating'];
        return $gameReviews;
    }
    
    function getGameComments($gameName)  {
        $query = 'SELECT * FROM comments WHERE game_name = "';
        $query .= $gameName . '"';
        $result = mysql_query($query) or die(mysql_error());
        $gameComments = array($gameName. ' Comments');
        //Store the results
        //$row = mysql_fetch_array($result);
        while ($row = mysql_fetch_array($result)) {
            $gameComments[] = $row['username'] . "|||" . $row['date'] . "|||" . $row['comment'];
        }
        
        return $gameComments;
    }
    
    function createAccount($firstName, $lastName, $email, $user, $pass) {
        $query = 'INSERT INTO users(first_name, last_name, email, username, password) VALUES("';
        $query .= $firstName. '", "' . $lastName. '", "' . $email. '", "' . $user. '", "' . $pass . '")';
        if (mysql_query($query)) {
            return true;
        } else {
            return false;
        }
    }
    
    function login($user, $pass) {
        $query = 'SELECT username FROM users WHERE username="';
        $query .= $user . '" AND password="' . $pass . '"';
        $result = mysql_query($query);
        return ($result && (mysql_num_rows($result) == 1));
    }
    
    function sendComment($username, $gameName, $reviewBody, $rating)  {
        $date = date('m/d/Y');
        $query = 'INSERT INTO comments(username, game_name, comment, rating, date) VALUES("';
        $query .= $username. '", "' . $gameName. '", "' . $reviewBody. '", "' . $rating. '",' . "NOW()" . ')';
        if (mysql_query($query)) {
            return true;
        } else {
            return false;
        }
    }
    
    function updateRatings($gameName, $metacriticRating, $gamespotRating, $ignRating)  {
        $query = 'UPDATE games ';
        $query .= 'SET metacritic_rating =  ' . $metacriticRating . ', ign_rating = '. $ignRating. ', gamespot_rating = ' . $gamespotRating . ' WHERE game_name = ' . "'" . $gameName . "'";
        
        echo $query;
        if (mysql_query($query))  {
            return true;
        } else  {
            return false;
        }
    }
    
    function getGameCount($gameName)  {
        $query = 'SELECT * FROM comments WHERE game_name = "';
        $query .= $gameName . '"';
        $result = mysql_query($query) or die(mysql_error());
        $count = mysql_num_rows($result);
        return $count;
    }
    
    function getTotalRating($gameName)  {
        $query = 'SELECT SUM(rating) FROM comments WHERE game_name = "';
        $query .= $gameName . '"';
        $result = mysql_query($query) or die(mysql_error());
        $row = mysql_fetch_array($result);
        $total = $row['SUM(rating)'];
        return $total ;
    }
    
    function addToFavorites($gameName, $username)  {
        $query = 'INSERT INTO favorites(game_name, username) VALUES("';
        $query .= $gameName. '", "' . $username. '")';
        if (mysql_query($query)) {
            return true;
        } else {
            return false;
        }
    }
    
    function getFavorites($username) {
        $query = 'SELECT * FROM favorites WHERE username = "';
        $query .= $username. '"';
        $result = mysql_query($query) or die(mysql_error());
        $gameList = array();
        // Store the results of the query
        while ($row = mysql_fetch_array($result)) {
            $gameList[] = $row['game_name'];
        }
        return $gameList;
    }
    
    function getSearchResults($gameName) {
        $query = 'SELECT * FROM games WHERE game_name like "';
        $query .= "%" . $gameName ."%" . '"';
        $result = mysql_query($query) or die(mysql_error());
        $gameList = array();
        // Store the results of the query
        while ($row = mysql_fetch_array($result)) {
            $gameList[] = $row['game_name'];
        }
        return $gameList;
    }

  function updateFavoritesCount($gameName, $count)  {
      $query = 'UPDATE games ';
      $query .= 'SET num_favorite =  ' . $count . ' WHERE game_name = ' . "'" . $gameName . "'";
      
      if (mysql_query($query))  {
          return true;
      } else  {
          return false;
      }
  }
    
    function getNumFav($gameName)  {
      $query = 'SELECT num_favorite FROM games WHERE game_name = "';
      $query .= $gameName . '"';
      $result = mysql_query($query) or die(mysql_error());
      $row = mysql_fetch_array($result);
      $total = $row['num_favorite'];
      return $total ;
    }
    
    function updateViewsCount($gameName, $count)  {
        $query = 'UPDATE games ';
        $query .= 'SET num_views =  ' . $count . ' WHERE game_name = ' . "'" . $gameName . "'";
        
        if (mysql_query($query))  {
            return true;
        } else  {
            return false;
        }
    }
    
    function getNumViews($gameName)  {
        $query = 'SELECT num_views FROM games WHERE game_name = "';
        $query .= $gameName . '"';
        $result = mysql_query($query) or die(mysql_error());
        $row = mysql_fetch_array($result);
        $total = $row['num_views'];
        return $total ;
    }
    ?>					